package com.anywhere.freightman.order.controller;

import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.order.CompanyQueryEntity;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.common.enumeration.RoleType;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.common.ex.ForbiddenException;
import com.anywhere.freightman.common.request.PageRequest;
import com.anywhere.freightman.order.controller.request.CompanyRequest;
import com.anywhere.freightman.order.controller.resp.CompanyDetailsResp;
import com.anywhere.freightman.order.controller.resp.CompanyResp;
import com.anywhere.freightman.order.model.CompanyModel;
import com.anywhere.freightman.order.service.ICompanyService;
import com.anywhere.freightman.system.model.UserModel;
import com.anywhere.freightman.system.service.IUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("company")
@Api(tags = "企业管理")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IUserService userService;

    @Permission(code = "COMPANY")
    @GetMapping("page")
    @ApiOperation("分页")
    public PageInfo<CompanyResp> getPageList(HttpServletRequest httpRequest,
                                             CompanyQueryEntity qm) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        PageRequest<CompanyQueryEntity> pageRequest = new PageRequest<>();
        if (!account.getType().equals(RoleType.ADMIN)) {
            throw new ForbiddenException();
        }
        BeanUtils.copyProperties(qm, pageRequest);
        pageRequest.setEntity(qm);
        PageInfo<CompanyModel> pageList = companyService.getPageList(pageRequest);
        PageInfo<CompanyResp> resp = new PageInfo<>();
        BeanUtils.copyProperties(pageList, resp);
        List<CompanyResp> list = new ArrayList<>();
        pageList.getList().forEach(item -> {
            CompanyResp response = new CompanyResp();
            BeanUtils.copyProperties(item, response);
            list.add(response);
        });
        resp.setList(list);

        return resp;
    }

    @Permission(code = "COMPANY")
    @GetMapping("{id}")
    @ApiOperation("详情")
    public ReturnResp getDetails(HttpServletRequest httpRequest,
                                 @PathVariable("id") Long id) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        if (!account.getType().equals(RoleType.ADMIN)) {
            throw new ForbiddenException();
        }

        CompanyModel model = companyService.getById(id);
        if (model == null) {
            throw new CustomException("企业信息不存在");
        }
        CompanyDetailsResp resp = new CompanyDetailsResp();
        BeanUtils.copyProperties(model, resp);
        List<UserModel> userList = userService.getListByCompanyId(resp.getId());
        resp.setUsername("");
        userList.forEach(item -> {
            if (item.getType().equals(RoleType.TENANT)) {
                resp.setUsername(resp.getUsername() + item.getUsername() + "; ");
            }
        });
        return ReturnResp.buildOk(resp);
    }

    @Permission(code = "COMPANY")
    @PostMapping
    @ApiOperation("新增企业")
    public ReturnResp add(HttpServletRequest httpRequest,
                          @RequestBody CompanyRequest requestEntity) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        if (!account.getType().equals(RoleType.ADMIN)) {
            throw new ForbiddenException();
        }
        companyService.insert(requestEntity);
        return ReturnResp.buildOk(requestEntity);
    }

    @Permission(code = "COMPANY")
    @PutMapping
    @ApiOperation("编辑企业")
    public ReturnResp edit(HttpServletRequest httpRequest,
                           @RequestBody CompanyRequest requestEntity) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        if (!account.getType().equals(RoleType.ADMIN)) {
            throw new ForbiddenException();
        }
        CompanyModel model = companyService.getById(requestEntity.getId());
        if (model == null) {
            throw new CustomException("企业信息不存在");
        }

        BeanUtils.copyProperties(requestEntity, model);
        companyService.update(model);
        return ReturnResp.buildOk(requestEntity);
    }

    @Permission(code = "COMPANY")
    @DeleteMapping("{id}")
    @ApiOperation("删除企业")
    public ReturnResp delete(@PathVariable("id") Long id,
                             HttpServletRequest httpRequest) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        if (!account.getType().equals(RoleType.ADMIN)) {
            throw new ForbiddenException();
        }

        CompanyModel model = companyService.getById(id);
        if (model == null) {
            throw new CustomException("企业信息不存在");
        }
        companyService.delete(model);
        return ReturnResp.buildOk();
    }
}
